/**   
  * @Description: TODO(用一句话描述该文件做什么) 
  * @author wby  
  * @date 2019年6月21日 下午9:30:38 
  * @version V1.0   
  */
package com.wing.jpa.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wing.jpa.entity.User;

@Repository
public interface UserRepository extends JpaRepository<User, String> {

	List<User> findByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2);

	List<User> findByLoginnameStartingWithOrAgeBetweenOrderByAgeDesc(String loginnameStartWith, int age1, int age2);

	Page<User> findByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2,
			Pageable pageable);

	User findTopByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2);

	User findFirstByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2);

	Page<User> findTop3ByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2,
			Pageable pageable);

	List<User> findFirst3ByLoginnameStartingWithOrAgeBetween(String loginnameStartWith, int age1, int age2);

	@Modifying
	@Query("update User u set u.name = ?1 where u.id = ?2")
	/**
	 * @Description: TODO(dosth)
	 * @param name
	 * @param id
	 * @return 返回的修改了多少条数据
	 */
	int modifyByIdAndUserId(String name, String id);

	@Transactional
	@Modifying
	@Query("delete from User where id = ?1")
	void deleteByUserId(String id);

	@Transactional(timeout = 10)
	@Query("select u from User u where u.id like ?1")
	List<User> findByIdLike(String idl);

	@Transactional(timeout = 10)
	@Query("select u from User u where u.id like ?1")
	Page<User> findByIdLike(String idl, Pageable page);

	@Transactional(timeout = 10)
	@Query(value = "select id,age,name,loginname,birth from user u where u.id like ?1", nativeQuery = true)
	/**
	 * @Description: 注意，字段和返回的对象要完全匹配，要不然报错不执行
	 * @param idl
	 * @return
	 */
	List<User> findByIdLikeNative(String idl);

	@Transactional(timeout = 10)
	@Query(value = "select id,age,name,loginname,birth from user u where u.id like ?1", countQuery = "select count(id) from user u where u.id like ?1", nativeQuery = true)
	Page<User> findByIdLikeNative(String idl, Pageable page);

}
